package com.hmy.common.base;

import com.hmy.common.utils.Messages;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果
 */

public class JsonResult<T> implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 8104304929896037489L;

    public static final Logger logger = LoggerFactory.getLogger(JsonResult.class);

    public static final String SUCCESS = "0";
    public static final String FAIL = "1";
    public static final String FAIL_MESSAGE = "未知错误";
    public static final String SUCCESS_MESSAGE = "操作成功";
    /**
     * 返回代码
     */
    private String code;
    /**
     * 错误信息,根据code从国际化文件里获取
     */
    private String msg;
    /**
     * 返回的数据
     */
    private T data;

    private Map<String, T> defaultData = new HashMap<>();

    public JsonResult() {

    }

    public JsonResult(String code) {
        this.code = code;
    }

    public JsonResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        if (null != data) {
            this.data = data;
        } else {
            this.data = (T) defaultData;
        }
    }

    /**
     * 是否成功
     *
     * @return
     */
    public boolean checkSuccess() {
        return SUCCESS.equals(code);
    }

    public static JsonResult success() {
        return new JsonResult(SUCCESS, SUCCESS_MESSAGE, null);
    }

    public static JsonResult success(Object data) {
        return new JsonResult(SUCCESS, null, data);
    }

    public static JsonResult success(String message, Object data) {
        return new JsonResult(SUCCESS, message, data);
    }

    public static JsonResult fail() {
        return new JsonResult(FAIL, FAIL_MESSAGE, null);
    }

    public static JsonResult fail(String code) {
        return fail(code, null);
    }

    public static JsonResult failMsg(String code, String... param) {
        String message = Messages.get(String.valueOf(code));
        if (StringUtils.isNotBlank(message) && null != param && param.length > 0) {
            for (int i = 0; i < param.length; i++) {
                message = message.replace("{" + i + "}", null == param[i] ? "" : param[i]);
            }

        }
        return new JsonResult(code, message, null);
    }

    public static JsonResult fail(String code, String message, Object data) {
        if (StringUtils.isBlank(message)) {
            String message1 = Messages.get(String.valueOf(code));
            return new JsonResult(code, message1, data);
        }
        return new JsonResult(code, message, data);
    }


    public static JsonResult fail(String code, Object data) {
        String message = Messages.get(String.valueOf(code));
        return new JsonResult(code, message, data);
    }

    public static JsonResult failCode(String code, String message) {
        return new JsonResult(code, message, null);
    }


    public JsonResult put(String key, T value) {
        defaultData.put(key, value);
        return this;
    }


    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("code", code)
                .append("msg", msg)
                .append("data", data)
                .toString();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
